<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { guild_roomApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from "element-plus";
  import Pagination from "@/components/Pagination/index.vue";
  import xmImage from '@/components/xmImage/index.vue';
  import unionForm from './components/unionForm.vue';
  import moreList from './components/moreList.vue';

  const unionFormRef = ref(),
        moreListRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const initialQuery = {
    page:1,
    limit:20,
    uuid:'',
    status: -1,
    user_uuid:'',
    name: null,
    send_status:-1,
    reward_type:-1
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const settleType = [
    {key:1,label:'普通'},
    {key:2,label:'对公结算'},
  ]
  const aptitude = [
    {key:0,label:'未认证'},
    {key:1,label:'个人认证'},
    {key:2,label:'企业认证'},
  ]
  const rewardType = [
    {key:0,label:'周结奖励'},
    {key:1,label:'实时奖励'},
  ]
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      ...initialQuery
    })
    getList()
  }
  const handleDeductDialog = (row:any={},formType=0)=>{
    unionFormRef.value.openDialog({
      ...row,
      formType,
      aptitude,
      settleType,
      rewardType
    })
  }
  const handleRelieveUnion = (row:any = {})=>{
    ElMessageBox.confirm("确定解散该公会吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      guild_roomApi.delUnionAll(row).then(()=>{
        ToastSuccess("操作成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const handleMore = (row:any = {})=>{
    moreListRef.value.openDialog({...row,rewardType})
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await guild_roomApi.getUnionList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">公会UUID：</div>
          <el-input
            v-model="listQuery.uuid"
            placeholder="请输入公会UUID"
          />
        </div>
        <div class="labelItem">
          <div class="label">会长UUID：</div>
          <el-input
            v-model="listQuery.user_uuid"
            placeholder="请输入会长会长UUID"
          />
        </div>
        <div class="labelItem">
          <div class="label">公会名称：</div>
          <el-input
            v-model="listQuery.name"
            placeholder="请输入公会名称"
          />
        </div>
        <div class="labelItem">
          <div class="label">资质认证：</div>
          <el-select v-model="listQuery.send_status" clearable placeholder="请选择消息状态">
            <el-option label="全部" :value="-1"/>
            <el-option
              v-for="item in aptitude"
              :key="item.key"
              :label="item.label"
              :value="item.key"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">奖励类型：</div>
          <el-select v-model="listQuery.reward_type" placeholder="请选择">
            <el-option label="全部" :value="-1" />
            <el-option
              v-for="item in rewardType"
              :key="item.key"
              :label="item.label"
              :value="item.key" />
          </el-select>
        </div>
      </div>
      <div class="labelBox">
        <el-button type="primary" @click="handleSerach">查询</el-button>
        <el-button type="primary" @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleDeductDialog()" >创建公会</el-button>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="180" prop="created_at" label="日期" />
      <el-table-column width="140" prop="send_nums" label="图片">
        <template #default="scope">
          <xmImage
            :src="scope.row.portrait"
            :list="[scope.row.portrait]"
            style="width: 100px; height: 100px"/>
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="id" label="公会ID" />
      <el-table-column min-width="180" prop="uuid" label="公会UUID">
        <template #default="scope">
          <el-tag class="pointer" @click="handleDeductDialog(scope.row,3)">{{ scope.row.uuid }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="left" min-width="180" prop="name" label="公会名">
        <template #default="scope">
          <el-tag class="pointer" @click="handleDeductDialog(scope.row,6)">
            {{ scope.row.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="user_uuid" label="会长UUID" />
      <el-table-column min-width="180" prop="nickname" label="会长昵称" />
      <el-table-column min-width="180" prop="rankVal" label="公会财富等级" />
      <el-table-column min-width="180" prop="rankActiveVal" label="公会活跃等级" />
      <el-table-column min-width="180" prop="authentication_status" label="资质认证">
        <template #default="scope">
          <el-tag class="pointer" @click="handleDeductDialog(scope.row,5)">
            {{ aptitude.find(res=>res.key == scope.row.authentication_status)?.label || '' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column min-width="120" label="结算类型">
        <template #default="scope">
          {{ settleType.find(res=> res.key == scope.row.settle_type)?.label || ''}}
        </template>
      </el-table-column>
      <el-table-column min-width="120" prop="reward_type" label="奖励类型">
        <template #default="scope">
          {{ rewardType.find(res=> res.key == scope.row.reward_type)?.label || ''}}
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="sort" label="排序" />
      <el-table-column min-width="180" prop="liang" label="靓标">
        <template #default="scope">
          <el-tag class="pointer" @click="handleDeductDialog(scope.row,4)">{{ scope.row.liang?'是':'否' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip min-width="180" prop="notice" label="公告" />
      <el-table-column show-overflow-tooltip min-width="180" prop="desc" label="描述" />
      <el-table-column width="240" fixed="right" label="操作">
        <template #default="scope">
        <el-row>
          <el-col class="xm-mb10" :span="12">
            <el-button type="primary" @click="handleDeductDialog(scope.row,1)" >编辑公会</el-button>
          </el-col>
          <el-col class="xm-mb10" v-if="scope.row.settle_type == 2" :span="12">
            <el-button type="primary" @click="handleDeductDialog(scope.row,2)" >编辑对公</el-button>
          </el-col>
          <el-col :span="12">
            <el-button type="danger" @click="handleRelieveUnion(scope.row)">解散公会</el-button>
          </el-col>
          <el-col :span="12">
            <el-button type="primary" @click="handleMore(scope.row)" >更多功能</el-button>
          </el-col>
        </el-row>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
      <unionForm ref="unionFormRef" @update-list="getList" />
      <moreList ref="moreListRef" @update-list="getList"  />
  </div>
</template>

<style lang='scss' scoped>

</style>